package com.lm.elastic.config;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticsearchConfig {

//    @Value("${spring.elasticsearch.uris}")
//    private String esHost;
//
//    @Value("${spring.elasticsearch.username}")
//    private String username;
//
//    @Value("${spring.elasticsearch.password}")
//    private String password;

    /**
     * 初始化 Elasticsearch 客户端
     */
//    @Bean
//    public ElasticsearchClient elasticsearchClient() {
//
//        // 设置认证
//        final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//        credentialsProvider.setCredentials(AuthScope.ANY,
//                new UsernamePasswordCredentials("elastic", "changeme"));
//
//        // 创建 RestClient
//        RestClient restClient = RestClient.builder(HttpHost.create("http://192.168.157.129:9200"))
//                .setHttpClientConfigCallback(httpClientBuilder ->
//                        httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
//                                .setSSLHostnameVerifier((hostname, session) -> true))
//                .build();
//
//        // 使用 Jackson 作为 JSON Mapper
//        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
//
//        // 创建 Elasticsearch 客户端
//        return new ElasticsearchClient(transport);
////        ElasticsearchClient client = new ElasticsearchClient(
////                new RestClientTransport(
////                        RestClient.builder(new HttpHost("localhost", 9200, "http")).build(),
////                        new JacksonJsonpMapper()
////                )
////        );
////        return client;
//    }

    @Bean
    public ElasticsearchClient elasticsearchClient() {

        final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials("elastic", "changeme"));

        RestClient restClient = RestClient.builder(HttpHost.create("http://192.168.157.129:9200"))
                .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder
                        .setDefaultCredentialsProvider(credentialsProvider)
                        .setSSLHostnameVerifier((hostname, session) -> true))
                .build();

        ElasticsearchTransport transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());

        return new ElasticsearchClient(transport);
    }
}
